﻿using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.ServiceModel.DomainServices.EntityFramework;

namespace CarRental.DataAccessLayer
{
    public class Wcf : LinqToEntitiesDomainService<CarRentalEntities>
    {
        CarRentalEntities __carRentalEntities = new CarRentalEntities();
        public Wcf(){
            
        }

        #region user
        public IQueryable<user> GetUser() {
            return ObjectContext.user;
        }

        public void InsertUser(user pUser) {
            if (pUser.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pUser, EntityState.Added);
            } else {
                __carRentalEntities.user.AddObject(pUser);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion

        #region car
        public IQueryable<car> GetCar() {
            return ObjectContext.car;
        }

        public void InsertCar(car pCar) {
            if (pCar.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pCar, EntityState.Added);
            } else {
                __carRentalEntities.car.AddObject(pCar);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion

        #region CarType
        public IQueryable<car_type> GetCarType() {
            return ObjectContext.car_type;
        }

        public void InsertCar(car_type pCarType) {
            if (pCarType.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(pCarType, EntityState.Added);
            } else {
                __carRentalEntities.car_type.AddObject(pCarType);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();
        }
        #endregion

        #region shop
        public IQueryable<shop> GetShop() {
            return ObjectContext.shop;
        }

        public void InsertShopByTest() {
            shop _shop = new shop();
            _shop.shopcode = "A1";
            _shop.shopname = "CarShop 1";
            _shop.address = "Hong Kong";

            if (_shop.EntityState != EntityState.Detached) {
                __carRentalEntities.ObjectStateManager.ChangeObjectState(_shop, EntityState.Added);
            } else {
                __carRentalEntities.shop.AddObject(_shop);
            }
            __carRentalEntities.SaveChanges();
            __carRentalEntities.Dispose();

            //using (__carRentalEntities = new CarRentalEntities()) {
            //    if (_shop.EntityState != EntityState.Detached)
            //    {
            //        __carRentalEntities.ObjectStateManager.ChangeObjectState(_shop, EntityState.Added);
            //    }
            //    else
            //    {
            //        __carRentalEntities.shop.AddObject(_shop);
            //    }
            //    __carRentalEntities.SaveChanges();
            //}
        }
        #endregion

        #region Depot
        public IQueryable<depot> GetDepot() {
            return ObjectContext.depot;
        }
        #endregion

        #region Drivetrains
        public IQueryable<drivetrain> GetDrivetrains() {
            return ObjectContext.drivetrain;
        }
        #endregion
    }
}